#include <iostream>
#include <string>
#include <thread>
#include <etcd/Client.hpp>
#include <etcd/KeepAlive.hpp>
#include <etcd/Response.hpp>

int main()
{
    std::string host_url = "http://127.0.0.1:2379";
    //实例化客户端对象
    etcd::Client client(host_url);
    //获取租约保活对象---伴随着创建一个指定有效时长的租约
    pplx::task<std::shared_ptr<etcd::KeepAlive>> lease_keep_alive = client.leasekeepalive(5);
    std::shared_ptr<etcd::KeepAlive> keep_alive = lease_keep_alive.get();
    //获取租约ID
    int64_t lease_id = keep_alive->Lease();
    /*向etcd服务器中新增数据---etcd服务器不止对键值对进行存储，还以目录的方式管理键值对，比如/service/user 
    和 /service/friend 中 user和friend都在service目录下, 如果以service为key获取会将service目录下的user和friend都获取出来*/
    pplx::task<etcd::Response> pplx_resp1 = client.put("/service/user", "127.0.0.1:8080", lease_id);
    etcd::Response resp1 = pplx_resp1.get();
    if(!resp1.is_ok())
    {
        std::cout << "新增数据失败： " << resp1.error_message() << std::endl;
        return -1;
    }

    pplx::task<etcd::Response> pplx_resp2 = client.put("/service/friend", "127.0.0.1:9090");
    etcd::Response resp2 = pplx_resp2.get();
    if(!resp2.is_ok())
    {
        std::cout << "新增数据失败： " << resp2.error_message() << std::endl;
        return -1;
    }
    //让这个线程休眠10s
    std::this_thread::sleep_for(std::chrono::seconds(10));
    return 0;
}

